package com.autumn.zero.authorization.entitys.users;

import javax.persistence.Column;
import javax.persistence.Table;

import org.apache.ibatis.type.JdbcType;
import org.hibernate.validator.constraints.Length;

import com.autumn.constants.SettingConstants;
import com.autumn.domain.entities.auditing.AbstractGmtCreateEntity;
import com.autumn.mybatis.mapper.annotation.ColumnOrder;
import com.autumn.mybatis.mapper.annotation.ColumnType;
import com.autumn.mybatis.mapper.annotation.Index;
import com.autumn.mybatis.mapper.annotation.TableEngine;
import com.autumn.util.StringUtils;

/**
 * 用户登录日志
 * 
 * @author 杨昌国 2018-11-29 23:00:18
 */
@Table(name = SettingConstants.SYS_TABLE_PREFIX + "_user_login_log")
@TableEngine("MyISAM")
public class UserLoginLog extends AbstractGmtCreateEntity<Long> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1865539580666139525L;

	/**
	 * 最大用户账户长度
	 */
	public static final int MAX_USER_ACCOUNT_LENGTH = 50;

	/**
	 * 最大客户端ip地址长度
	 */
	public static final int MAX_CLIENT_IP_ADDRESS_LENGTH = 64;

	/**
	 * 最大客户端名称长度
	 */
	public static final int MAX_CLIENT_NAME_LENGTH = 128;

	/**
	 * 最大浏览信息长度
	 */
	public static final int MAX_BROWSER_INFO_LENGTH = 500;

	/**
	 * 字段 userId
	 */
	public static final String FILED_USER_ID = "userId";

	/**
	 * 字段 userAccount
	 */
	public static final String FILED_USER_ACCOUNT = "userAccount";

	/**
	 * 字段 provider
	 */
	public static final String FILED_PROVIDER = "provider";

	/**
	 * 字段 providerKey
	 */
	public static final String FILED_PROVIDER_KEY = "providerKey";

	/**
	 * 字段 clientIpAddress
	 */
	public static final String FILED_CLIENT_IP_ADDRESS = "clientIpAddress";

	/**
	 * 字段 clientName
	 */
	public static final String FILED_CLIENT_NAME = "clientName";

	/**
	 * 字段 browserInfo
	 */
	public static final String FILED_BROWSER_INFO = "browserInfo";

	/**
	 * 用户id
	 */
	@ColumnOrder(1)
	@Index(unique = false)
	private Long userId;

	/**
	 * 用户账号
	 */
	@Length(max = MAX_USER_ACCOUNT_LENGTH, message = "用户账号长度不能超过" + MAX_USER_ACCOUNT_LENGTH + "个字。")
	@Column(name = "user_account", nullable = false, length = MAX_USER_ACCOUNT_LENGTH)
	@ColumnOrder(2)
	@Index(unique = false)
	private String userAccount;

	/**
	 * 第三方提供者
	 */
	@Length(max = UserExternalAuthLogin.MAX_PROVIDER_LENGTH, message = "第三方提供者名称长度不能超过"
			+ UserExternalAuthLogin.MAX_PROVIDER_LENGTH + "个字。")
	@Column(name = "provider", nullable = false, length = UserExternalAuthLogin.MAX_PROVIDER_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(3)
	@Index(unique = false)
	private String provider;

	/**
	 * 第三方提供者键
	 */
	@Length(max = UserExternalAuthLogin.MAX_PROVIDER_KEY_LENGTH, message = "第三方提供者键长度不能超过"
			+ UserExternalAuthLogin.MAX_PROVIDER_KEY_LENGTH + "个字。")
	@Column(name = "provider_key", nullable = false, length = UserExternalAuthLogin.MAX_PROVIDER_KEY_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(4)
	private String providerKey;

	/**
	 * 客户端Ip地址
	 */
	@Length(max = MAX_CLIENT_IP_ADDRESS_LENGTH, message = "客户端Ip地址长度不能超过" + MAX_CLIENT_IP_ADDRESS_LENGTH + "个字。")
	@Column(name = "client_ip_address", nullable = false, length = MAX_CLIENT_IP_ADDRESS_LENGTH)
	@ColumnOrder(5)
	@Index(unique = false)
	private String clientIpAddress;

	/**
	 * 客户端名称
	 */
	@Length(max = MAX_CLIENT_NAME_LENGTH, message = "客户端名称长度不能超过" + MAX_CLIENT_NAME_LENGTH + "个字。")
	@Column(name = "client_name", nullable = false, length = MAX_CLIENT_NAME_LENGTH)
	@ColumnOrder(6)
	private String clientName;

	/**
	 * 浏览信息
	 */
	@Length(max = MAX_BROWSER_INFO_LENGTH, message = "浏览信息长度不能超过" + MAX_BROWSER_INFO_LENGTH + "个字。")
	@Column(name = "browser_info", nullable = false, length = MAX_BROWSER_INFO_LENGTH)
	@ColumnOrder(7)
	private String browserInfo;

	/**
	 * 获取用户Id
	 * 
	 * @return
	 */
	public Long getUserId() {
		return userId;
	}

	/**
	 * 设置用户Id
	 * 
	 * @param userId 用户Id
	 */
	public void setUserId(Long userId) {
		this.userId = userId;
	}

	/**
	 * 获取用户账号
	 * 
	 * @return
	 */
	public String getUserAccount() {
		return userAccount;
	}

	/**
	 * 设置用户账号
	 * 
	 * @param userAccount 用户账号
	 */
	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	/**
	 * 获取提供者
	 * 
	 * @return
	 */
	public String getProvider() {
		return provider;
	}

	/**
	 * 设置提供者
	 * 
	 * @param provider 提供者
	 */
	public void setProvider(String provider) {
		this.provider = provider;
	}

	/**
	 * 获取提供者键
	 * 
	 * @return
	 */
	public String getProviderKey() {
		return providerKey;
	}

	/**
	 * 设置提供者键
	 * 
	 * @param providerKey 提供者键
	 */
	public void setProviderKey(String providerKey) {
		this.providerKey = providerKey;
	}

	/**
	 * 获取客户端Ip地址
	 * 
	 * @return
	 */
	public String getClientIpAddress() {
		return clientIpAddress;
	}

	/**
	 * 设置客户端Ip地址
	 * 
	 * @param clientIpAddress 客户端Ip地址
	 */
	public void setClientIpAddress(String clientIpAddress) {
		this.clientIpAddress = clientIpAddress;
	}

	/**
	 * 获取客户端名称
	 * 
	 * @return
	 */
	public String getClientName() {
		return clientName;
	}

	/**
	 * 设置客户端名称
	 * 
	 * @param clientName 客户端名称
	 */
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	/**
	 * 获取浏览信息
	 * 
	 * @return
	 */
	public String getBrowserInfo() {
		return browserInfo;
	}

	/**
	 * 设置浏览信息
	 * 
	 * @param browserInfo 浏览信息
	 */
	public void setBrowserInfo(String browserInfo) {
		this.browserInfo = browserInfo;
	}

	/**
	 * 截断字符串
	 */
	public void truncationString() {
		this.setProvider(this.getMaxLength(this.getProvider(), UserExternalAuthLogin.MAX_PROVIDER_LENGTH));
		this.setProviderKey(this.getMaxLength(this.getProviderKey(), UserExternalAuthLogin.MAX_PROVIDER_KEY_LENGTH));
		this.setUserAccount(this.getMaxLength(this.getUserAccount(), MAX_USER_ACCOUNT_LENGTH));
		this.setClientIpAddress(this.getMaxLength(this.getClientIpAddress(), MAX_CLIENT_IP_ADDRESS_LENGTH));
		this.setClientName(this.getMaxLength(this.getClientName(), MAX_CLIENT_NAME_LENGTH));
		this.setBrowserInfo(this.getMaxLength(this.getBrowserInfo(), MAX_BROWSER_INFO_LENGTH));
	}

	private String getMaxLength(String value, int maxLength) {
		if (value == null) {
			return "";
		}
		value = value.trim();
		return StringUtils.getLeft(value, maxLength);
	}
}
